package com.gemme.gemme.util;

import android.content.Context;

import com.hyphenate.EMCallBack;
import com.hyphenate.chat.ChatClient;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.hyphenate.helpdesk.callback.Callback;

/**
 * @Author: yangcheng
 * @CreateDate: 2020/9/15 14:09
 */
public class EMClientUtil {
    //先注册--后登陆
    public static void registeredAndLogin(Context context,String username,String pwd){
        new Thread(new Runnable() {
            @Override
            public void run() {
                // call method in SDK
                try {
                    EMClient.getInstance().createAccount(username, pwd);

                    //传入在应用（appkey）下注册的IM用户的账号和密码，用于登录环信服务器
                    EMClient.getInstance().login(username,pwd,new EMCallBack() {//回调
                        @Override
                        public void onSuccess() {
                            EMClient.getInstance().groupManager().loadAllGroups();
                            EMClient.getInstance().chatManager().loadAllConversations();

                        }

                        @Override
                        public void onProgress(int progress, String status) {
                            System.out.println();
                        }

                        @Override
                        public void onError(int code, String message) {
                            System.out.println();
                        }
                    });
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    //登陆
    public static void login(Context context,String username,String pwd){
        new Thread(new Runnable() {
            @Override
            public void run() {
                // call method in SDK
                try {
//                    EMClient.getInstance().createAccount(username, pwd);

                    //传入在应用（appkey）下注册的IM用户的账号和密码，用于登录环信服务器
                    EMClient.getInstance().login(username,pwd,new EMCallBack() {//回调
                        @Override
                        public void onSuccess() {
                            EMClient.getInstance().groupManager().loadAllGroups();
                            EMClient.getInstance().chatManager().loadAllConversations();

                        }

                        @Override
                        public void onProgress(int progress, String status) {
                            System.out.println();
                        }

                        @Override
                        public void onError(int code, String message) {
                            System.out.println();
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    //环信客服云注册登录
    public static void registeredAndLoginBykefu(Context context,String username,String pwd){
        new Thread(new Runnable() {
            @Override
            public void run() {
                // call method in SDK
                ChatClient.getInstance().register(username, pwd, new Callback(){
                    @Override
                    public void onSuccess() {//注册成功后登陆

                        System.out.println("");
                        loginBykefu(context,username, pwd);
                    }

                    @Override
                    public void onError(int code, String error) {
                        System.out.println("");
                    }

                    @Override
                    public void onProgress(int progress, String status) {
                        System.out.println("");
                    }
                });
            }
        }).start();
    }

    //客服登录
    public static void loginBykefu(Context context,String username,String pwd){
        new Thread(new Runnable() {
            @Override
            public void run() {
                // call method in SDK
                ChatClient.getInstance().login(username, pwd, new Callback(){
                    @Override
                    public void onSuccess() {
                        System.out.println();
                    }

                    @Override
                    public void onError(int code, String error) {
                        System.out.println();

                    }

                    @Override
                    public void onProgress(int progress, String status) {
                        System.out.println();

                    }
                });
            }
        }).start();
    }


}
